import numpy as np


np.random.seed(0)
arr = np.random.randint(10, 50, size=(4, 5))
col_means = np.mean(arr, axis=0)
row_maxs = np.max(arr, axis=1)

print("原始数组:")
print(arr)
print("\n每列平均值:", col_means)
print("每行最大值:", row_maxs)

arr[(arr >= 25) & (arr < 35)] = 0
print("\n替换[25,35)后的数组:")
print(arr)

# 对数组按行标准化
normalized = np.zeros_like(arr, dtype=float)
for i in range(arr.shape[0]):
    row_min = np.min(arr[i])
    row_max = np.max(arr[i])
    # 处理全行相同的情况（避免除零）
    if row_max == row_min:
        normalized[i] = 0
    else:
        normalized[i] = (arr[i] - row_min) / (row_max - row_min)

print("\n标准化后的数组:")
print(normalized)

# 找出第2列(索引1)最大的3个值所在行索引
column_1 = normalized[:, 1]
top3_idx = np.argsort(column_1)[-3:]

# 使用行索引提取组成新数组
new_arr = arr[top3_idx]

# 计算协方差矩阵 (按列计算)
cov_matrix = np.cov(new_arr, rowvar=False)

print("\n标准化后第2列最大的3个值索引:", top3_idx)
print("\n提取的行组成的新数组:")
print(new_arr)
print("\n新数组的协方差矩阵:")
print(cov_matrix)